Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dsa-week1-assignment 윤영서 #2

Merged
merged 15 commits into from
Sep 4, 2023
Merged

Conversation

YeongseoYoon
Copy link
Contributor

풀리퀘 많이 안해봐서 어렵네용,,,
앞으로 6주 잘 부탁드립니당~

Copy link
Contributor

@hannut91 hannut91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생 많으셨습니다

Comment on lines +19 to 25
const solution = (numbers, index = 0) => {
if (index >= numbers.length) {
return 0;
} else {
return numbers[index] + solution(numbers, index + 1);
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아마 for문이 지금 제일 익숙해서 index를 이용해서 해결해주신 것 같아요. 빈 배열이 주어진 경우와, 배열이 하나씩 줄어둔다는 것을 이용할 수 있다는 것도 알아두시면 좋을 것 같습니다.

const solution = (numbers) => {
  if (numbers.length === 0) {
    return 0;
  }

  return numbers[0] + solution(numbers.slice(1));
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slice 메소드를 사용할 수 있다는건 생각도 안했네요 ㅠㅠ 감사합니다 ㅎㅎ

Comment on lines +18 to 34
const solution = (n) => {
let pre = 0;
let curr = 1;
let temp = 0;
if (n <= -1) {
return 0;
} else if (n === 0 || n === 1) {
return n;
}
while (n >= 2) {
temp = pre;
pre = pre + curr;
curr = temp;
n--;
}
return pre + curr;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

프로그램을 더 이상 실행할 필요가 없을 경우 빠르게 종료하도록 하면, 아래 코드를 더 이상 읽지 않아도 되기도 하고 불필요한 코드 실행도 막을 수 있어요. 게다가 진짜 코드의 의도는 아래에 배치하여 의도를 드러낼 수도 있습니다.

const solution = (n) => {
  if (n <= -1) {
    return 0;
  }
  
  if (n === 0 || n === 1) {
    return n;
  }
  
  let pre = 0;
  let curr = 1;
  let temp = 0;

  while (n >= 2) {
    temp = pre;
    pre = pre + curr;
    curr = temp;
    n--;
  }

  return pre + curr;
};

See also

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

얼리리턴을 생각해놓고는 할당을 하고 얼리리턴을 해줘버렸네요 ㅎㅎ 감사합니다!!

}

while (exponent <= length - 1) {
acc += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문자열 마지막 문자를 숫자로 변경한 후에 제곱을 더해주고 있는 것 같아요. 문자열 마지막이라면 index로 접근해도 좋을 것 같아요

Suggested change
acc += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent;
acc += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent;


const length = n.length;
if (length === 1) {
return acc + parseInt(n, 10) * 2 ** exponent;
Copy link
Contributor

@hannut91 hannut91 Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

연산자의 우선순위는 프로그래밍 언어마다 다를수도 있어요. 그래서 a + b + c같이 아주 자명한 경우가 아니라면 적절히 괄호로 묶어주는 것이 좋습니다.

    return acc + parseInt(n, 10) * (2 ** exponent);

@hannut91 hannut91 reopened this Sep 4, 2023
@hannut91 hannut91 merged commit e5ccb85 into CodeSoom:main Sep 4, 2023
0 of 2 checks passed
hannut91 added a commit that referenced this pull request Sep 4, 2023
This reverts commit e5ccb85, reversing
changes made to 4349bc1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants